<template>
  <div>
    <el-card style="margin:20px 0;">
      <CategorySelect @getCategoryId="getCategoryId" :show="scene != 0"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <!-- 展示spu列表的结构 -->
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table border style="width: 100%;" :data="records">
          <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="col.width"></el-table-column>
          <el-table-column prop="description" label="spu描述" width="col.width"></el-table-column>
          <el-table-column prop="col.id" label="操作" width="col.width">
            <template slot-scope='{row,$index}'>
              <hint-button type="success" size="mini" icon="el-icon-plus" title="添加sku"
                @click="addSku(row)"></hint-button>
              <hint-button type="warning" size="mini" icon="el-icon-edit" title="修改spu"
                @click="updateSpu(row)"></hint-button>
              <hint-button type="info" size="mini" icon="el-icon-info" title="查看当前spu全部sku列表"
                @click="handler(row)"></hint-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination style="text-align: center;" :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit"
          layout="prev, pager, next, jumper, ->,sizes,total" :total="total" @current-change="currentChange"
          @size-change="sizeChange" :pager-count="5">
        </el-pagination>

      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu"></SpuForm>
      <SkuForm v-show="scene == 2" @changeScenes="changeScenes" ref="sku"></SkuForm>
    </el-card>
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <!-- table展示sku列表-->
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row,$index}">
            <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px;">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'
export default {
  name: 'Spu',
  components: { SpuForm, SkuForm },
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      show: true,
      page: 1,//代表当前第几页
      limit: 3,//代表每一页展示多少条数据
      records: [],//spu列表的数据
      total: 0,//分页器总共数据
      scene: 0,//0代表展示spu列表数据   1代表添加SPU|修改SPU   2代表添加SKU
      dialogTableVisible: false,//控制对话框显示或隐藏
      spu: {},
      skuList: [], //存储的是SKU列表的数据
      loading: true
    }
  },
  methods: {
    getCategoryId({ categoryId, level }) {
      //categoryId:可以获取到一二三级分类的id   level:是为了区分几级id
      if (level == 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
        this.records = []
      } else if (level == 2) {
        this.category2Id = categoryId
        this.category3Id = ""
        this.records = []
      } else {
        this.category3Id = categoryId
        //获取spu列表数据进行展示
        this.getSpuList()
      }
    },
    //获取spu列表数据的方法
    async getSpuList() {
      const { page, limit, category3Id } = this
      //需要携带三个参数:page/limit/三级分类id
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      if (result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    //点击第几页分页的回调
    currentChange(page) {
      this.page = page
      this.getSpuList()
    },
    //当某一页展示数据条数发生变化
    sizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    //添加spu
    addSpu() {
      this.scene = 1
      this.$refs.spu.addSpuData(this.category3Id)
    },
    //修改某一个spu
    updateSpu(row) {
      this.scene = 1
      this.$refs.spu.initSpuData(row)
    },
    //自定义事件回调spuform
    changeScene({ scene, flag }) {
      this.scene = scene
      if (flag == '修改') {
        this.currentChange(this.page)
      } else {
        this.currentChange(1)
      }
    },
    //删除spu的回调
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        //代表SPU个数大于1删除的时候停留在当前页，如果SPU个数小于1 回到上一页
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    //添加sku
    addSku(row) {
      this.scene = 2
      //父组件调用子组件的方法，让子组件发请求------三个请求
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    //skuform通知父组件修改场景
    changeScenes(scene) {
      this.scene = scene;
    },
    //查看sku按钮的回调
    async handler(spu) {
      //显示对话框
      this.dialogTableVisible = true
      this.spu = spu
      //获取sku列表的数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        //loading隐藏
        this.loading = false;
      }
    },
    //关闭对话框的回调
    close(done) {
      //loading属性再次变为真
      this.loading = true;
      //清除sku列表的数据
      this.skuList = [];
      //管理对话框
      done();
    }
  },
}
</script>
<style scoped></style>
